package net.zretc.controller;

import net.zretc.entity.Order;
import net.zretc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public List<Order> findAll() {
        try {
            return orderService.findAll();
        } catch (Exception e) {
            System.err.println("获取订单列表出错: " + e.getMessage());
            return null;
        }
    }

    @GetMapping("/user/{userId}")
    public List<Order> findByUserId(@PathVariable Integer userId) {
        try {
            return orderService.findByUserId(userId);
        } catch (Exception e) {
            System.err.println("获取用户订单出错: " + e.getMessage());
            return null;
        }
    }

    @PostMapping
    public int insert(@RequestBody Order order) {
        try {
            return orderService.insert(order);
        } catch (Exception e) {
            System.err.println("插入订单出错: " + e.getMessage());
            return 0;
        }
    }

    @PutMapping
    public int update(@RequestBody Order order) {
        try {
            return orderService.update(order);
        } catch (Exception e) {
            System.err.println("更新订单出错: " + e.getMessage());
            return 0;
        }
    }

    @DeleteMapping("/{orderId}")
    public int delete(@PathVariable Integer orderId) {
        try {
            return orderService.delete(orderId);
        } catch (Exception e) {
            System.err.println("删除订单出错: " + e.getMessage());
            return 0;
        }
    }
}